package com.odais.json;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Lionel DECELLE
 * @since 8 juin 2012 - 10:52:29
 *
 */
public class JSON_IO_Convert{

	private static final String JSON_RETURN_STRING_SUCCESS = "success";
	private static final String JSON_RETURN_STRING_MSG = "txtmsg";
	private static final String JSON_RETURN_STRING_NEW_ID = "seller";
	private static final String JSON_RETURN_STRING_NUMBER = "number";
	

	public static String JSON_DB_Action[] = {	"getResDB",
										"SaveReDB",
										"sql",
									   	"getSelDB",
									   	"AddSelDB",
									   	"available",
									   	"c2dm_id",
									   	"update_password"};


//	 public String JSON_DB_getResDB[] = {"seller",
//										 "numrsv",
//										 "status",
//									   	 "numtbl",
//									   	 "nbrpla",
//									   	 "datrdv",
//									   	 "heurdv",
//									   	 "heumin",
//									   	 "heumax"};

	public JSONObject JSON_IO_Convert_To_JSON(String[] DataTable){

		//Create JSON object
    	JSONObject j = new JSONObject();
    	try {
	    	for(int i=0; i<(int)((DataTable.length/2)); i++)
	    	{
	        	j.put(DataTable[2*i], DataTable[2*i+1]);
	    	}
    	} catch (JSONException e1) {
    	    e1.printStackTrace();
    	}
    	return j;
	}

	public HashMap<String, Object> JSON_IO_Convert_To_String(JSONObject j){

		HashMap<String, Object> DataTableOut = new HashMap<String, Object>();
	    HashMap<String, String> DataTableOuttmp = new HashMap<String, String>();

		DataTableOuttmp.put(JSON_RETURN_STRING_SUCCESS, "false");
		DataTableOuttmp.put(JSON_RETURN_STRING_MSG, "No valid response from server");

		boolean ActionValid;
		int checkAction;


    	try {
    		ActionValid = false;
    		checkAction = -1;
    		//Check if action is valid
    		while(checkAction<JSON_DB_Action.length-1){
    			ActionValid = j.has(JSON_DB_Action[++checkAction]);
    			if(ActionValid) break;
    		}

    		if(ActionValid)
    		{
	    		// Get the return array
			    JSONArray returnTableArr = j.optJSONArray(JSON_DB_Action[checkAction]); //Action
			    JSONObject returnTable = returnTableArr.getJSONObject(0);
			    JSONArray returnTableNames = returnTable.names();

			    // Checks how many objects are in the JSON stream
			    int JSONCnt = returnTableNames.length();

			    // build output table.


			    //Common part
		    	if(returnTable.has(JSON_RETURN_STRING_SUCCESS))
		    		DataTableOuttmp.put(JSON_RETURN_STRING_SUCCESS, returnTable.optString(JSON_RETURN_STRING_SUCCESS));

		    	if(returnTable.has(JSON_RETURN_STRING_MSG))
		    		DataTableOuttmp.put(JSON_RETURN_STRING_MSG, returnTable.optString(JSON_RETURN_STRING_MSG));

			    switch(checkAction){
				    case 0 : 	// "getResDB" txtmsg is reservation count
				    	//Create table of json arrays
					    JSONObject returnResTableObj = new JSONObject();
					    JSONArray returnResTableNames = new JSONArray();
					    int JSONCntbis;

				    	for(int i=0; i<(JSONCnt-2); i++){
				    		returnResTableObj = returnTable.getJSONObject(Integer.toString(i));
				    		returnResTableNames = returnResTableObj.names();
				    		JSONCntbis = returnResTableNames.length();

				    		// Fetch these values
				    		//["numrsv","nbrpla","heumax","status","heumin","datrdv","seller","numtbl","heurdv"]
				    		//

				    		//Create output table
				    		for(int k=0; k<JSONCntbis; k++){
					    		DataTableOuttmp.put(returnResTableNames.get(k).toString(), returnResTableObj.optString(returnResTableNames.get(k).toString()));
				    		}
				    		DataTableOut.put(Integer.toString(i), DataTableOuttmp.clone());
				    	}

				    	break;
				    case 1 : 	// "SaveReDB"
			    		DataTableOut.put(Integer.toString(0), DataTableOuttmp.clone());
				    	break;
				    case 4 :	// "AddCliDB"
				    	if(returnTable.has(JSON_RETURN_STRING_NEW_ID))
				    		DataTableOuttmp.put(JSON_RETURN_STRING_NEW_ID, returnTable.optString(JSON_RETURN_STRING_NEW_ID));
				    	else
			    			DataTableOuttmp.put(JSON_RETURN_STRING_NEW_ID, "ndef");

			    		DataTableOut.put(Integer.toString(0), DataTableOuttmp.clone());
				    	break;
				    case 7 : 	// "update_password"
				    	
				    	//hack : update_password returns "number":0 if failed
				    	if(returnTable.has(JSON_RETURN_STRING_NUMBER) && returnTable.optString(JSON_RETURN_STRING_NUMBER).equals("0"))
				    		DataTableOuttmp.put(JSON_RETURN_STRING_SUCCESS, "false");
				    	else
			    			DataTableOuttmp.put(JSON_RETURN_STRING_SUCCESS, "true");
				    	
			    		DataTableOut.put(Integer.toString(0), DataTableOuttmp.clone());
				    	break;
				    default: break;
			    }

    		}
    		else
    		{
	    		//DataTableOut.put(JSON_RETURN_STRING_SUCCESS, "No valid response");
	    		//DataTableOut.put(JSON_RETURN_STRING_MSG, "No valid response");
	    		DataTableOut.put("0", DataTableOuttmp.clone());
    		}
    	} catch (JSONException e) {

    		//DataTableOut.put(JSON_RETURN_STRING_SUCCESS, "Exception");
    		//DataTableOut.put(JSON_RETURN_STRING_MSG, "Exception");
    		DataTableOut.put("0", DataTableOuttmp.clone());
    	}
	    return DataTableOut;
	}

}